//
//  QAIM.m
//  Pods-QAGameIM_Example
//
//  Created by 范浩云 on 2018/3/13.
//

#import "QAIMGame.h"
#import "QAIMViewController.h"
static QAIMGame *_gImInstance;


@interface QAIMGame () <UINavigationControllerDelegate>
{
    BOOL _mainViewLoad;
    
    id<QAIMGameDelegate> _delegate;
    id<UINavigationControllerDelegate> _oldDelegate;
    UIResponder *_parentResponder;
}

@property (nonatomic, strong) QAIMViewController *aImController;

@end


@implementation QAIMGame

+ (instancetype)getImInstance
{
    if (_gImInstance == nil) {
        _gImInstance = [[[self class] alloc] init];
    }
    return _gImInstance;
}


+ (void)showInResponder:(UIResponder *)sender
               withInfo:(NSDictionary *)userInfo
               delegate:(id<QAIMGameDelegate>)delegate;

{
    [[self getImInstance] showInResponder:sender withInfo:userInfo delegate:delegate];
}

+ (id<QAIMGameDelegate>)getDelegate;
{
    return [[self getImInstance] getDelegate];
}


+ (void)distory
{
    [[self getImInstance] distory];
}
#pragma mark-- instance method

- (void)distory
{
    _aImController = nil;
    _aImController.navigationController.delegate = _oldDelegate;
    UIViewController *controller = [_aImController.navigationController popViewControllerAnimated:YES];
    if (controller == nil) {
        [_aImController dismissViewControllerAnimated:YES completion:nil];
    }
}


- (id<QAIMGameDelegate>)getDelegate
{
    return _delegate;
}


- (void)showInResponder:(UIResponder *)sender withInfo:(NSDictionary *)userInfo delegate:(id<QAIMGameDelegate>)delegate
{
    _delegate = delegate;
    
    if (_aImController == nil) {
        _aImController = [[QAIMViewController alloc] init];
        //        _aImController.title = @"波波游戏IM";
        
    }
    UIViewController *currentController = _aImController;
    
    if (currentController.parentViewController == nil) {
        if ([sender isKindOfClass:[UIView class]]) {
            UIView *parentView = (UIView *)sender;
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:currentController];
            navController.delegate = self;
            navController.navigationBarHidden = YES;
            navController.view.frame = parentView.bounds;
            [parentView addSubview:navController.view];
        } else if ([sender isKindOfClass:[UINavigationController class]]) {
            UINavigationController *parentController = (UINavigationController *)sender;
            _oldDelegate = parentController.delegate;
            parentController.delegate = self;
            [parentController pushViewController:currentController animated:YES];
        } else if ([sender isKindOfClass:[UIViewController class]]) {
            UIViewController *parentController = (UIViewController *)sender;
            
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:currentController];
            navController.delegate = self;
            navController.navigationBarHidden = YES;
            [parentController presentViewController:navController animated:YES completion:^{
                
            }];
        }
    }
    
    _parentResponder = sender;
}


#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController isKindOfClass:[QAIMViewController class]]) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        navigationController.navigationBar.hidden = YES;
    } else {
        //        navigationController.navigationBar.hidden = NO;
    }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
}


+ (NSString *)SDKVersion
{
    return @"";
}
@end

